<template>
  <div>
    <h1 style="font-size: 28px">新增站点</h1>
    <div>
      <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
        <a-form-item label="地址" required>
          <a-input v-model:value="address"/>
        </a-form-item>
        <a-form-item label="经度">
          <a-input v-model:value="geo_lng"/>
        </a-form-item>
        <a-form-item label="纬度">
          <a-input v-model:value="geo_lat"/>
        </a-form-item>
        <a-form-item label="描述">
          <a-textarea v-model:value="intro"/>
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" style="width: 140px;" @click="onConfirm">确定</a-button>
          <a-button style="width: 140px;margin-left: 20px;" @click="onBack">返回</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts">
import {defineComponent, reactive, toRefs} from "vue";
import {createSite} from "/@/api/product/site";
import {message} from "ant-design-vue";
import {HttpResult} from "/@/api/core/model/coreModel";

export default defineComponent({
  name: 'SiteAdd',
  props: {
    viewType: {
      type: String,
      default: null,
    }
  },
  setup(props, {emit}) {

    const data = reactive({
      address: "",
      geo_lng: "",
      geo_lat: "",
      intro: "",
    });

    const onConfirm = () => {
      if (data.address.length == 0) {
        message.warn("请输入有效的地址信息");
        return;
      }
      createSite({
        address: data.address,
        geo_lng: data.geo_lng,
        geo_lat: data.geo_lat,
        intro: data.intro
      }).then((result: HttpResult) => {
        if (result.code == 0) {
          message.success("新增成功");
          emit("my-emit", 'Confirm');
        } else {
          message.error(result.message);
        }
      });
    };

    const onBack = () => {
      emit("my-emit", 'Cancel');
    }

    return {
      ...toRefs(data),
      onConfirm,
      onBack
    };
  }
});

</script>

<style scoped>

</style>
